package com.smartleanx.module.hrm.controller.admin.employee;

import com.smartleanx.framework.common.util.date.DateUtils;
import com.smartleanx.module.hrm.controller.admin.employee.contract.*;
import com.smartleanx.module.hrm.controller.admin.employee.edu.EmployeeEducationExperienceRespVO;
import com.smartleanx.module.hrm.dal.dataobject.employee.EmployeeEducationExperienceDO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;

import com.smartleanx.framework.common.pojo.CommonResult;
import com.smartleanx.framework.common.util.object.BeanUtils;

import static com.smartleanx.framework.common.pojo.CommonResult.success;

import com.smartleanx.module.hrm.dal.dataobject.employee.EmployeeContractDO;
import com.smartleanx.module.hrm.service.employee.EmployeeContractService;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Tag(name = "管理后台 - 员工合同")
@RestController
@RequestMapping("/hrm/employee-contract")
@Validated
public class EmployeeContractController {

    @Resource
    private EmployeeContractService employeeContractService;

    @PostMapping("/create")
    @Operation(summary = "创建员工合同")
    @PreAuthorize("@ss.hasAnyPermissions('hrm:employee-contract:create','hrm:employee:update')")
    public CommonResult<Long> createEmployeeContract(@Valid @RequestBody EmployeeContractSaveReqVO createReqVO) {
        return success(employeeContractService.createEmployeeContract(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新员工合同")
    @PreAuthorize("@ss.hasAnyPermissions('hrm:employee-contract:update','hrm:employee:update')")
    public CommonResult<Boolean> updateEmployeeContract(@Valid @RequestBody EmployeeContractSaveReqVO updateReqVO) {
        employeeContractService.updateEmployeeContract(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除员工合同")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasAnyPermissions('hrm:employee-contract:delete','hrm:employee:update')")
    public CommonResult<Boolean> deleteEmployeeContract(@RequestParam("id") Long id) {
        employeeContractService.deleteEmployeeContract(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得员工合同")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasAnyPermissions('hrm:employee-contract:query','hrm:employee:query')")
    public CommonResult<EmployeeContractRespVO> getEmployeeContract(@RequestParam("id") Long id) {
        return success(employeeContractService.getEmployeeContract(id));
    }

    @GetMapping("/getContractSimple")
    @Operation(summary = "实时计算员工合同结束日期、状态")
    @PreAuthorize("@ss.hasAnyPermissions('hrm:employee-contract:query','hrm:employee:query')")
    public CommonResult<EmployeeContractSimpleRespVo> getContractSimple(@RequestParam(value = "year", required = false) Integer year,
                                                                        @RequestParam(value = "startTime", required = false) Long startTime) {
        return success(employeeContractService.getContractSimple(year, startTime));
    }

    @GetMapping("/getContractStatus")
    @Operation(summary = "获得员工合同执行状态")
    @PreAuthorize("@ss.hasAnyPermissions('hrm:employee-contract:query','hrm:employee:query')")
    public CommonResult<Integer> contractStatus(@Valid EmployeeContractRepVo repVo) {
        return success(employeeContractService.contractStatus(repVo));
    }

    @GetMapping("/list")
    @Operation(summary = "根据员工id获得员工合同列表")
    @Parameter(name = "employeeId", description = "员工编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasAnyPermissions('hrm:employee-contract:query','hrm:employee:query')")
    public CommonResult<List<EmployeeContractRespVO>> getEmployeeContractList(@RequestParam("employeeId") Long employeeId) {
        return success(employeeContractService.getEmployeeContractList(employeeId));
    }

    @PostMapping("/terminationContract")
    @Operation(summary = "终止合同")
//    @PreAuthorize("@ss.hasAnyPermissions('hrm:employee-contract:update')")
    public CommonResult<Boolean> terminationContract(@RequestBody ContractTerminationSaveRepVO saveRepVO) {
        employeeContractService.terminationContract(saveRepVO);
        return success(true);
    }

}
